package com.opengroupe.dao.impl;

// Generated 31 juil. 2012 15:44:44 by Hibernate Tools 3.4.0.CR1

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.opengroupe.dao.CandidatDao;
import com.opengroupe.entities.Candidat;

/**
 * Home object for domain model class Candidat.
 * @see com.opengroupe.entities.Candidat
 * @author Hibernate Tools
 */
@Stateless
public class CandidatDaoImpl extends JpaDaoSupport implements CandidatDao {

	private static final Log log = LogFactory.getLog(CandidatDaoImpl.class);

	public Candidat persist(Candidat transientInstance) throws Exception {
		log.debug("persisting Candidat instance");
		try {
			getJpaTemplate().persist(transientInstance);
			log.debug("persist successful");
			return transientInstance;
		} catch (Exception e) {
			log.error("persist failed", e);
			throw e;
		}
	}

	public void remove(Candidat persistentInstance) {
		log.debug("removing Candidat instance");
		try {
			getJpaTemplate().remove(persistentInstance);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public Candidat merge(Candidat detachedInstance) throws Exception{
		log.debug("merging Candidat instance");
		try {
			Candidat result = getJpaTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Candidat findById(Integer id) {
		log.debug("getting Candidat instance with id: " + id);
		try {
			Candidat instance = getJpaTemplate().find(Candidat.class, id);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("rawtypes")
	public Candidat findByEmail(String email) {
		try {
			List candidats = getJpaTemplate().findByNamedQuery("findByEmail", email);
			if(candidats.size() > 0) {
				return (Candidat) candidats.get(0);
			} else {
				return null;
			}
		} catch (RuntimeException  e) {
			log.error("get by email failed", e);
			throw e;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Candidat> findByNomPrenom(String nom, String prenom) {
		log.debug("getting Candidat instance with nom: " + nom + " and prenom : "+prenom);
		try {
			List<Candidat> candidats = (List<Candidat>)getJpaTemplate().findByNamedQuery("findByNomPrenom", nom, prenom);
			return candidats;
		} catch (RuntimeException e) {
			log.error("get by nom and prenom failed", e);
		}
		return null;
	}

	@Transactional
	public void deleteCascade(final String emailCandidat) {
		JpaCallback<Candidat> jpaCallback = new JpaCallback<Candidat>() {

			@Override
			public Candidat doInJpa(EntityManager entityManager)
					throws PersistenceException {
				String strQuery = "delete CandidatReponseQuestion as crq where " +
						"crq.candidat.idCandidat=some(select c.idCandidat from " +
						"Candidat as c where c.emailCandidat=:email)";
				Query query = entityManager.createQuery(strQuery);
				query.setParameter("email", emailCandidat);
				query.executeUpdate();
				
				strQuery = "delete Candidat as c where c.emailCandidat=:email";
				query = entityManager.createQuery(strQuery);
				query.setParameter("email", emailCandidat);
				query.executeUpdate();
				return null;
			}
		};
		getJpaTemplate().execute(jpaCallback);
	}

	@Override
	public List<Candidat> findAll() {
		log.debug("getting all Candidats");
		try{
			@SuppressWarnings("unchecked")
			List<Candidat> candidats = (List<Candidat>)getJpaTemplate().findByNamedQuery("findAllCandidats");
			return candidats;
		} catch (RuntimeException e){
			log.error("get all candidats failed", e);
		}
		return null;
	}

}
